package com.nexgo.payment.trans.setting.sysparam;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.SwitchButton;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 系统参数设置界面
 */
public class SetupSystemActivity extends SaveParameterActivity {

    @BindView(R.id.trace_number)
    EditText traceNumber;//流水号
    @BindView(R.id.batch_number)
    EditText batchNumber;//批次号
    @BindView(R.id.open_voice_prompt)
    SwitchButton openVoicePrompt;//是否开启语音提示
    @BindView(R.id.order_need_english)
    SwitchButton orderNeedEnglish;//是否支持打印英文
    @BindView(R.id.void_class_need_print_minus_sign)
    SwitchButton voidClassNeedPrintMinusSign;//是否打印负号
    @BindView(R.id.print_times)
    EditText printTimes;//打印联数
    @BindView(R.id.max_number_of_trans)
    EditText maxNumberOfTrans;//最大交易笔数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_system);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.system_parameter_settings))
                .build();
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.TRACE_NUMBER, traceNumber.getText().toString());
        map.put(PreferencesConstants.BATCH_NUMBER, batchNumber.getText().toString());
        map.put(PreferencesConstants.OPEN_VOICE_PROMPT, openVoicePrompt.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.ORDER_NEED_ENGLISH, orderNeedEnglish.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.VOID_CLASS_NEED_PRINT_MINUS_SIGN, voidClassNeedPrintMinusSign.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRINT_TIMES, printTimes.getText().toString());
        map.put(PreferencesConstants.MAX_NUMBER_OF_TRANS, maxNumberOfTrans.getText().toString());
        return map;
    }

    @Override
    public void showDetail() {
        traceNumber.setText(mPresenter.getParamValue(PreferencesConstants.TRACE_NUMBER));
        batchNumber.setText(mPresenter.getParamValue(PreferencesConstants.BATCH_NUMBER));
        openVoicePrompt.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.OPEN_VOICE_PROMPT)));
        orderNeedEnglish.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.ORDER_NEED_ENGLISH)));
        voidClassNeedPrintMinusSign.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.VOID_CLASS_NEED_PRINT_MINUS_SIGN)));
        printTimes.setText(mPresenter.getParamValue(PreferencesConstants.PRINT_TIMES));
        maxNumberOfTrans.setText(mPresenter.getParamValue(PreferencesConstants.MAX_NUMBER_OF_TRANS));
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                if (mPresenter != null) {
                    mPresenter.saveParams(getDataBind());
                }
                break;
            default:
                break;
        }
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupSystemActivity.class);
    }
}
